{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab-02-1 linear regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "tf.set_random_seed(777)  # for reprducibilty"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## X and Y data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_train = [1, 2, 3]\n",
    "y_train = [1, 2, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Try to find value for W and b to compute y_data = x_data * W + b  \n",
    "# We know that W should be 1 and b should be 0\n",
    "# But let's TensorFlow figure it out \n",
    "W = tf.Variable(tf.random_normal([1]), name='weight')\n",
    "b = tf.Variable(tf.random_normal([1]), name='bias')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Our Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Out hypothesis XW+b\n",
    "hypothesis = x_train * W + b\n",
    "\n",
    "# cost/loss function\n",
    "cost = tf.reduce_mean(tf.square(hypothesis - y_train))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Minimize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)\n",
    "train = optimizer.minimize(cost)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Launch the graph in a session.\n",
    "sess = tf.Session()\n",
    "# Initializes global variables in the graph.\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fit the line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 2.82329 [ 2.12867713] [-0.85235667]\n",
      "200 0.0699668 [ 1.30721486] [-0.69837117]\n",
      "400 0.0267167 [ 1.18983996] [-0.4315508]\n",
      "600 0.0102017 [ 1.11730957] [-0.26667204]\n",
      "800 0.00389553 [ 1.07249022] [-0.16478711]\n",
      "1000 0.0014875 [ 1.04479456] [-0.10182849]\n",
      "1200 0.000567998 [ 1.02768016] [-0.06292368]\n",
      "1400 0.000216891 [ 1.01710474] [-0.03888312]\n",
      "1600 8.28196e-05 [ 1.01056981] [-0.02402747]\n",
      "1800 3.16242e-05 [ 1.00653136] [-0.01484741]\n",
      "2000 1.20761e-05 [ 1.00403607] [-0.00917497]\n"
     ]
    }
   ],
   "source": [
    "for step in range(2001):\n",
    "    sess.run(train)\n",
    "    if step % 200 == 0:\n",
    "        print(step, sess.run(cost), sess.run(W), sess.run(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Learns best fit W:[ 1.], b:[  0.]\n",
    "\n",
    "```\n",
    "0 3.51465 [-0.03875808] [ 0.40560108]\n",
    "200 0.0286106 [ 0.80354649] [ 0.44658491]\n",
    "400 0.010925 [ 0.87860364] [ 0.27596244]\n",
    "600 0.00417169 [ 0.92498434] [ 0.17052816]\n",
    "800 0.00159296 [ 0.95364493] [ 0.10537602]\n",
    "1000 0.000608266 [ 0.97135544] [ 0.06511588]\n",
    "1200 0.000232263 [ 0.98229945] [ 0.04023758]\n",
    "1400 8.86917e-05 [ 0.98906201] [ 0.02486444]\n",
    "1600 3.38653e-05 [ 0.99324113] [ 0.01536462]\n",
    "1800 1.2932e-05 [ 0.99582338] [ 0.00949448]\n",
    "2000 4.9383e-06 [ 0.99741906] [ 0.00586706]\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
